﻿Imports s3pi.Interfaces
Imports s3pi.Package
Imports System.IO
Imports System.Drawing
Imports s3pi.WrapperDealer
Imports System.Text
Imports System.Reflection
Public Class Save
    Private FamilyIcon As Image
    Private LotIcon As Image
    Private mWorldName As String
    Private nhdPath As String
    Private mInstance As ULong
    'Private mFamilyName As String

    Public ReadOnly Property FamilyThum As Image
        Get
            Return FamilyIcon
        End Get
    End Property
    Public ReadOnly Property ImgInstance As ULong
        Get
            Return mInstance
        End Get
    End Property
    Public ReadOnly Property WorldName As String
        Get
            Return mWorldName
        End Get
    End Property
    Public ReadOnly Property Hash As String
        Get
            Return nhdPath.GetHashCode().ToString()
        End Get
    End Property
    'Public ReadOnly Property FamilyName As String
    '    Get
    '        Return mFamilyName
    '    End Get
    'End Property
    Public Sub New(strPath As String)
        nhdPath = strPath
        Dim path As String = IO.Path.Combine(IO.Path.GetDirectoryName(strPath), "Meta.data")
        Dim pack As Package = Package.OpenPackage(0, path)
        Dim entry As IResourceIndexEntry = pack.Find(Function(R As IResourceIndexEntry) R.ResourceType = &H628A788F)
        Dim s As Stream = WrapperDealer.GetResource(0, pack, entry).Stream
        UnParse(s)
    End Sub

    Private Sub UnParse(s As Stream)
        Dim r = New BinaryReader(s)

        Dim versionnumber As Integer = r.ReadInt32()
        Dim sb As New StringBuilder()
        Dim count As Integer = r.ReadInt32()
        For i As Integer = 0 To count - 1
            Dim c As Int16 = r.ReadInt16()
            sb.Append(ChrW(c))
        Next
        sb.Append(" ")
        For i As Integer = 0 To r.ReadInt32() - 1
            Dim c As Int16 = r.ReadInt16()
            sb.Append(ChrW(c))
        Next
        mWorldName = sb.ToString()
        r.ReadInt32()
        Dim nhd As IPackage = Package.OpenPackage(0, nhdPath)
        mInstance = r.ReadUInt64()
        Dim family As IResourceIndexEntry = nhd.Find(Function(entry As IResourceIndexEntry) entry.Instance = mInstance And entry.ResourceType = &H6B6D837E)
        If family IsNot Nothing Then
            FamilyIcon = Image.FromStream(WrapperDealer.GetResource(0, nhd, family).Stream)
        Else
            Dim asb As Assembly = Assembly.GetExecutingAssembly()

            FamilyIcon = Image.FromStream(asb.GetManifestResourceStream("Save_Cleaner.Unavailable.png"))
        End If

        Dim ul As ULong = r.ReadUInt64()


    End Sub

End Class
